package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListDetailMapper;
import com.snowfield.lb47.lbconfig.mapper.BlackwhiteListMapper;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 黑白名单配置Service业务层处理
 *
 * @author snowfield
 * @date 2024-05-24
 */
@Service
public class BlackwhiteListServiceImpl implements IBlackwhiteListService {
    @Autowired
    private BlackwhiteListMapper blackwhiteListMapper;
    @Autowired
    private BlackwhiteListDetailMapper blackwhiteListDetailMapper;

    /**
     * 查询黑白名单配置
     *
     * @param id 黑白名单配置主键
     * @return 黑白名单配置
     */
    @Override
    public BlackwhiteList selectBlackwhiteListById(Long id) {
        return blackwhiteListMapper.selectBlackwhiteListById(id);
    }

    /**
     * 查询黑白名单配置列表
     *
     * @param blackwhiteList 黑白名单配置
     * @return 黑白名单配置
     */
    @Override
    public List<BlackwhiteList> selectBlackwhiteListList(BlackwhiteList blackwhiteList) {
        return blackwhiteListMapper.selectBlackwhiteListList(blackwhiteList);
    }

    /**
     * 新增黑白名单配置
     *
     * @param blackwhiteList 黑白名单配置
     * @return 结果
     */
    @Override
    public int insertBlackwhiteList(BlackwhiteList blackwhiteList) {
        return blackwhiteListMapper.insertBlackwhiteList(blackwhiteList);
    }

    /**
     * 修改黑白名单配置
     *
     * @param blackwhiteList 黑白名单配置
     * @return 结果
     */
    @Override
    public int updateBlackwhiteList(BlackwhiteList blackwhiteList) {
        blackwhiteList.setUpdateTime(DateUtils.getNowDate());
        return blackwhiteListMapper.updateBlackwhiteList(blackwhiteList);
    }

    /**
     * 批量删除黑白名单配置
     *
     * @param ids 需要删除的黑白名单配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBlackwhiteListByIds(Long[] ids) {
        blackwhiteListDetailMapper.deleteBlackwhiteListDetailByListIds(ids);
        return blackwhiteListMapper.deleteBlackwhiteListByIds(ids);
    }

    /**
     * 删除黑白名单配置信息
     *
     * @param id 黑白名单配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBlackwhiteListById(Long id) {
        blackwhiteListDetailMapper.deleteBlackwhiteListDetailByListIds(new Long[]{id});
        return blackwhiteListMapper.deleteBlackwhiteListById(id);
    }

    /**
     * 校验黑白名单名称是否唯一
     *
     * @param blackwhiteList 黑白名单
     * @return 结果
     */
    public Boolean checkNameUnique(BlackwhiteList blackwhiteList) {
        Long listId = blackwhiteList.getId();
        BlackwhiteList list = blackwhiteListMapper.selectBlackwhiteListByName(blackwhiteList.getListName());
        if (list != null && !list.getId().equals(listId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
